# -*- coding: utf-8 -*-
# @#Time : 2021/8/22 17:17

# @File : hunanweishi_spider.py
# @Software : PyCharm
import requests
from bs4 import BeautifulSoup

import schedule_tool


def build_m3u_data(logo: str, category: str, channel_name: str, play_url: str):
    channel_data = '#EXTINF:-1,  tvg-logo="{}", group-title="{}", {}\n{}\n'.format(logo,
                                                                                   category,
                                                                                   channel_name,
                                                                                   play_url)
    return channel_data


class Spider:
    def __init__(self, url=None, header=None):
        self.source_url = url
        self.header = header

    def get_source(self):
        response = requests.get(url=self.source_url, headers=self.header)
        return response.text

    def parse(self):
        soup = BeautifulSoup(self.get_source(), features="html.parser")
        return soup.video['src']

    def save_file(self):
        pass


def output():
    url = 'http://m.hunanweishi.tv/'
    header = {
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'

    }
    spyder = Spider(url=url, header=header)

    logo = "http://zixun.hunantv.com/favicon.ico"
    category = '地方频道'
    channel_name = '湖南卫视-超清'
    play_url = spyder.parse()

    m3u_data = [build_m3u_data(logo, category, channel_name, play_url)]

    schedule_tool.schedule_process('hunanweishi.m3u', m3u_data)
#
if __name__ == '__main__':
    output()
